<?php

use backend\Backend;
use yii\helpers\ArrayHelper;

// 合并common中的配置信息
$config = require Yii::getAlias('@common/config/main.php');

$globPaths = [
    '@common/modules/*/config/main.php', // 合并modules下面的主配置信息
    '@common/modules/*/config/backend.php', // 合并modules下面的后台配置信息
    '@backend/modules/*/config/main.php', // 合并backend下面的后台配置信息
    '@backend/modules/*/config/backend.php', // 合并backend下面的后台配置信息
];
foreach ($globPaths as $globPath)
{
    $files = glob(Yii::getAlias($globPath));
    foreach ($files as $file)
    {
        $config = ArrayHelper::merge(
            require $file,
            $config
        );
    }
}

$config = ArrayHelper::merge($config, [
    'id' => 'app-cms-backend',
    'name' => '后台系统',
    'basePath' => CMS_ROOT . 'backend' . DIRECTORY_SEPARATOR,
    'runtimePath' => CMS_ROOT . 'backend' . DIRECTORY_SEPARATOR . 'runtime',
    'controllerNamespace' => 'backend\\controllers',
    'defaultRoute'        => 'admin/default/index',
    'modules' => [
        // 系统相关模块
        'setting' => [
            'class' => 'funson86\setting\Module',
            'controllerNamespace' => 'funson86\setting\controllers'
        ],

        // 界面相关的模块
        'gridview'=>[
            'class'=>'kartik\grid\Module',
            // other module settings
        ],
        'treemanager' =>  [
            'class' => '\kartik\tree\Module',
            // other module settings, refer detailed documentation
        ],
    ],
    'components' => [
        'request' => [
            'cookieValidationKey' => 'Djflj3*dfs',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                ''               => 'admin/default/index',
                Backend::$backendDir . '<controller:\w+>/<action:[\w-]+>/<id:\d+>' => '<controller>/<action>',
                Backend::$backendDir . '<module:\w+>/<controller:\w+>/<action:[\w-]+>/<id:\d+>' => '<module>/<controller>/<action>'
            ],
        ],
        'i18n' => [
            'translations' => [
                'backend' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'sourceLanguage' => 'en-US',
                    'basePath' => '@backend/messages',
                    'fileMap' => [
                        'backend' => 'backend.php',
                    ]
                ],
                'kvtree' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'sourceLanguage' => 'en-US',
                    'basePath' => '@common/messages',
                    'fileMap' => [
                        'kvtree' => 'kvtree.php',
                    ]
                ],
            ],
        ],
        'formatter' => [
            'sizeFormatBase' => 1000
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'view'                => [
            'theme' => 'backend\themes\Theme'
        ],
        'errorHandler' => [
            'class' => '\bedezign\yii2\audit\components\web\ErrorHandler',
            'errorAction' => 'admin/default/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@frontend/mail',
        ],
        'assetManager' => [
            'baseUrl' => 'http://'.$_SERVER['HTTP_HOST'].'/assets',
            'linkAssets' => true,
        ],
        'authManager' => [
            'class' => 'backend\components\RbacPhpManager',
            'defaultRoles' => [
                'user'
            ],
            'itemFile' => '@backend/config/rbac/data/items.php',
            'assignmentFile' => '@backend/config/rbac/data/assignments.php',
            'ruleFile' => '@backend/config/rbac/data/rules.php',
        ],
        "adminAccessLog"=>'backend\components\AdminAccessLog',
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
                //adminAccessLog
                [
                    'class' => 'yii\log\FileTarget',
                    'logFile' => "@runtime/logs/admin-access.log",
                    'levels' => ['info'],
                    'categories' => ['adminAccessLog'],
                    'logVars' => [],
                ],
            ],
        ],
        'sidebarMenuProvider' => [
            'class' => 'backend\components\SidebarMenuProvider',
            'itemDataPath'=>'@backend/config/menu/menu.php'
        ],
        "adminAuth"=>'backend\components\AdminAuth',
        "appMetaData"=> 'backend\components\AppMetaData',
    ],
    'bootstrap' => [
        'log',
        'backend\Bootstrap',
    ],
    'extensions' => require(CMS_ROOT . 'vendor/yiisoft/extensions.php'),
]);

// 测试环境额外配置
if (YII_ENV_DEV)
{
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'controllerNamespace' => 'backend\modules\gii\controllers',
        'layout' => '//main',
        'allowedIPs' => ['127.0.0.1'],
        'generators' => [
            'model' => [
                'class' => 'backend\modules\gii\generators\model\Generator'
            ],
            'crud' => [
                'class' => 'backend\modules\gii\generators\crud\Generator',
            ],
            'tree' => [
                'class' => 'backend\modules\gii\generators\tree\Generator',
            ],
        ]
    ];

    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class'      => 'yii\debug\Module',
        'allowedIPs' => [
            '127.0.0.1',
            '::1',
            '192.168.33.1',
        ],
    ];
}

return $config;
